package core.views;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import core.Runner;

/**
 * Brings up a part of the screen that displays the 'Buy' and 'Go to trail' buttons
 * 
 * @author 4c1a
 */
public class StartPanelMain {
    private static JPanel cards, mainPanel;
    private static JButton gameButton, goToTrail;
    private mainPanelListener listener;
    private Runner runner;
    private SetupPanel setupPanel;
    private static StorePanel storePanel;
    private static TitlePanel titlePanel;
    private static OptionsPanel optionsPanel;
    private static TrailPanel trailPanel;
    private static InventoryPanel invPanel;
    private static RiverPanel riverPanel;
    private static GamePanel gamePanel;
    private static CardLayout cl;
    private static ImageIcon icon = new ImageIcon("src/media/health.png");

    /**
     * Assigns a runner to the panel.
     * 
     * @param runner The runner to be assigned to the panel.
     */
    public StartPanelMain(Runner runner) {
    	this.runner = runner;
    }
    
    /**
     * Creates several buttons and sets the card layout for the game.
     * 
     * @param pane The pane to be used by the panel.
     */
    public void addComponentToPane(Container pane) {
        mainPanel = new JPanel();
        listener = new mainPanelListener();
        goToTrail = new JButton("Go to trail");
        gameButton = new JButton("Go to store");
        gameButton.setVisible(false);
        gameButton.addActionListener(listener);
        goToTrail.addActionListener(listener);
        mainPanel.add(gameButton);
         
        gamePanel = new GamePanel(runner);
        titlePanel = new TitlePanel(runner);
        setupPanel = new SetupPanel(runner);
        storePanel = new StorePanel(runner);
        optionsPanel = new OptionsPanel(runner);
        trailPanel = new TrailPanel(runner); 
        invPanel = new InventoryPanel(runner);
        riverPanel = new RiverPanel(runner);
        cards = new JPanel(new CardLayout());
        cards.add(titlePanel, "Title");
        cards.add(setupPanel, "Setup");
        cards.add(storePanel, "Store");
        cards.add(optionsPanel, "Options");
        cards.add(trailPanel, "Trail");
        cards.add(invPanel, "Inventory");
        cards.add(riverPanel, "River");
        cards.add(gamePanel, "Game");
        cl = (CardLayout)(cards.getLayout());
        pane.add(mainPanel, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
    }
     
    /**
     * Creates the GUI.
     * 
     * @param runner The runner to be used by the program.
     */
    private static void createAndShowGUI(Runner runner) {
        //Create and set up the window.
        JFrame frame = new JFrame("Oregon Trail");
        frame.setIconImage(icon.getImage());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
        //Create and set up the content pane.
        StartPanelMain mainpanel = new StartPanelMain(runner);
        mainpanel.addComponentToPane(frame.getContentPane());
         
        //Display the window.
		frame.setSize(450,370);
		frame.setVisible(true);
		frame.setResizable(false);
    }
     
    /**
     * Starts the GUI creation method.
     * 
     * @param runner The runner to be used by the GUI.
     */
    public static void run(Runner runner) {
    	createAndShowGUI(runner);
    }
    
    /**
     * Changes JPanels
     * 
     * @param card The name of the JPanel to change to
     */
    public static void changeCard(String card)
    {
    	if(card.equals("Inventory"))
    	{
    		invPanel.loadData();
    	}
    	else if(card.equals("Trail"))
    	{
    		trailPanel.loadData();
    	}
    	else if(card.equals("Store"))
    	{
    		storePanel.loadItemsPrice();
    	}
    	cl.show(cards, card);
    	//changeMainButton("trail");
    }
    
    /**
     * Changes the text of the main button
     * 
     * @param prevPanel the previous panel that was displayed
     */
    public static void changeMainButton(String prevPanel)
    {
    	if(prevPanel == "trail")
    	{
    		gameButton.setText("Buy");
    		goToTrail.setVisible(true);
    		goToTrail.setText("Trail");
    	}
    }
    
    public static void showButtons()
    {
    	gameButton.setVisible(true);
    	goToTrail.setVisible(true);
    }
    
    public static void showGameButton()
    {
    	gameButton.setVisible(true);
    }
    
    public static void hideGameButton()
    {
    	gameButton.setVisible(false);
    }
    /**
     * The listener for this panel.
     * 
     */
	private class mainPanelListener implements ActionListener
	{
		/**
		 * Performs the necessary action based on the source event.
		 * 
		 * @param e The source event to be processed.
		 */
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource()==gameButton)
			{
				if(gameButton.getText()=="Go to store")
				{
					setupPanel.update();
					cl.show(cards, "Store");
					
					storePanel.loadItemsPrice();
					gameButton.setText("Buy");
					mainPanel.add(goToTrail);
				}
				else if(gameButton.getText()=="Buy")
				{
					storePanel.update();
				}
				/*else if(gameButton.getText()=="Options")
				{
					optionsPanel.loadData();
					cl.show(cards,"Options");
					gameButton.setText("Trail");
				}*/
				else if(gameButton.getText()=="Trail")
				{
					cl.show(cards,"Trail");
					trailPanel.loadData();
					trailPanel.update();
					gameButton.setText("Show map");
				}
				else if(gameButton.getText()=="Show map")
				{
					//cl.show(cards, "Map")
				}
			}
			else if(e.getSource()==goToTrail)
			{
				if (readyForTrail())
				{
				trailPanel.loadData();
				goToTrail.setVisible(false);
				gameButton.setVisible(false);
				cl.show(cards, "Trail");
				}
			}
		}
		
		private boolean readyForTrail()
		{
			if ( runner.getGroup().getWagon().getInventory().getItemQuantity("Food") < 1)
			{
				JFrame y = new JFrame();
				JOptionPane
						.showMessageDialog(y, "You cannot hit the trail without any food!");
				return false;
			}
			else if(runner.getGroup().getWagon().getInventory().getItemQuantity("Oxen") <= 1)
			{
				JFrame y = new JFrame();
				JOptionPane
						.showMessageDialog(y, "You need at least 2 oxen to hit the trail!");
				return false;
			}
			return true;
		}
		
	}
}